package cn.xuewenbao.ucenter.controller;


import cn.xuewenbao.edu.entity.vo.IntroduceVo;
import cn.xuewenbao.ucenter.entity.Member;
import cn.xuewenbao.ucenter.entity.vo.*;
import cn.xuewenbao.ucenter.service.FollowService;
import cn.xuewenbao.ucenter.service.MemberDataService;
import cn.xuewenbao.ucenter.service.MemberService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author mafayu
 * @since 2021-03-29
 */
@RestController
@RequestMapping("/ucenter/member")
@Api(tags = "用户表")
@Slf4j
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberDataService memberDataService;

    //用户表-根据id获取用户信息
    @ApiOperation(value = "用户表-根据id获取用户信息")
    @GetMapping("getMemberInfo/{memberId}")
    public MemberInfoVo getMemberInfo(@PathVariable String memberId) {
        MemberInfoVo memberInfoVo = memberService.getMemberInfo(memberId);
        return memberInfoVo;
    }

    //用户表-根据id获取用户信息-用于个人简介
    @ApiOperation(value = "用户表-根据id获取用户信息-用于个人简介")
    @GetMapping("getMemberProfile/{memberId}/{userId}")
    public Result<MemberProfileVo> getMemberProfile(@PathVariable String memberId, @PathVariable String userId) {
        return memberService.getMemberProfile(memberId, userId);
    }


    @ApiOperation(value = "用户表-根据id获取用户数据")
    @GetMapping("getMemberData/{memberId}")
    public Result<?> getMemberData(@PathVariable String memberId) {
        return memberDataService.getMemberData(memberId);
    }

    //用户表-修改个人资料
    @ApiOperation(value = "用户表-修改个人资料")
    @PostMapping("updateMember")
    public boolean updateMember(@RequestBody MemberUpdate memberUpdate) {
        boolean flag = memberService.updateMember(memberUpdate);
        return flag;
    }

    //用户表-修改个人密码
    @ApiOperation(value = "用户表-修改个人密码")
    @PostMapping("changePassword")
    public Result<Member> changePassword(@RequestParam(name = "username") String username,
                                         @RequestParam(name = "password") String password,
                                         @RequestParam(name = "phone") String phone) {
        Result<Member> result = new Result<Member>();
        if (oConvertUtils.isEmpty(username) || oConvertUtils.isEmpty(password) || oConvertUtils.isEmpty(phone)) {
            result.setMessage("重置密码失败！");
            result.setSuccess(false);
            return result;
        }

        Member member = new Member();
        member = this.memberService.getOne(new LambdaQueryWrapper<Member>().eq(Member::getUsername, username).eq(Member::getMobile, phone));
        if (member == null) {
            result.setMessage("未找到用户！");
            result.setSuccess(false);
            return result;
        } else {
            String salt = oConvertUtils.randomGen(8);
            member.setSalt(salt);
            String passwordEncode = PasswordUtil.encrypt(phone, password, salt);
            member.setPassword(passwordEncode);
            this.memberService.updateById(member);
            result.setSuccess(true);
            result.setMessage("密码重置完成！");
            return result;
        }
    }

    //首页用户重置密码
    @ApiOperation(value = "用户表-首页用户重置密码")
    @PutMapping("updatePassword")
    public Result<?> changPassword(@RequestBody JSONObject json) {
        String mobile = json.getString("mobile");
        String oldpassword = json.getString("oldpassword");
        String password = json.getString("password");
        String confirmpassword = json.getString("confirmpassword");
        Member user = this.memberService.getOne(new LambdaQueryWrapper<Member>().eq(Member::getMobile, mobile));
        if (user == null) {
            return Result.error("用户不存在！");
        }
        return memberService.resetPassword(mobile, oldpassword, password, confirmpassword);
    }

//    //根据个人兴趣好物推荐
//    @ApiOperation(value = "根据个人兴趣好物推荐")
//    @GetMapping("personalIntroduce/{userId}")
//    public Result<List<IntroduceVo>> personalIntroduce(@PathVariable String userId){
//        Result<List<IntroduceVo>> result=memberService.personalIntroduce(userId);
//        return result;
//    }
}

